{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一、Day1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0.print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What is your name?Mike\n",
      "Hello Mike\n"
     ]
    }
   ],
   "source": [
    "name = input(\"What is your name?\")\n",
    "print(\"Hello \"+name )\n",
    "# 或者print(\"Hello\",name ),print中逗号分隔直接将字符串用空格分隔，若用+号连接，并且想留空格，则在前一字符串留空格即可"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.输入输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:111\n",
      "password:666\n",
      "111 666\n"
     ]
    }
   ],
   "source": [
    "username=input(\"username:\")\n",
    "password=input(\"password:\")\n",
    "print(username,password)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.格式输入输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Mike\n",
      "age:28\n",
      "job:worker\n",
      "---------info of ---------\n",
      "Name:Mike\n",
      "Age:28\n",
      "Job:worker\n"
     ]
    }
   ],
   "source": [
    "# 第一种方法\n",
    "name=input(\"Name:\")\n",
    "age=input(\"age:\")\n",
    "job=input(\"job:\")\n",
    "\n",
    "info='''---------info of ---------''' + '''\n",
    "Name:'''+name+'''\n",
    "Age:'''+age+'''\n",
    "Job:'''+job\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Mike\n",
      "age:28\n",
      "job:worker\n",
      "---------info of ---------\n",
      "Name:Mike\n",
      "Age:28\n",
      "Job:worker\n"
     ]
    }
   ],
   "source": [
    "# 第二种方法\n",
    "\n",
    "name=input(\"Name:\")\n",
    "age=int(input(\"age:\"))  #如果不用int()就会报错(虽然输入为数字，但是print(type(age))为str型)，因为python如果不强制类型转化，就会默认字符型\n",
    "job=input(\"job:\")\n",
    "\n",
    "info='''---------info of ---------\n",
    "Name:%s\n",
    "Age:%d\n",
    "Job:%s'''%(name,age,job)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Mike\n",
      "age:28\n",
      "job:worker\n",
      "---------info of ---------\n",
      "Name:Mike\n",
      "Age:28\n",
      "Job:worker\n"
     ]
    }
   ],
   "source": [
    "# 第三种方法\n",
    "\n",
    "name=input(\"Name:\")\n",
    "age=int(input(\"age:\"))  #如果不用int()就会报错(虽然输入为数字，但是print(type(age))为str型)，因为python如果不强制类型转化，就会默认字符型\n",
    "job=input(\"job:\")\n",
    "\n",
    "info='''---------info of ---------\n",
    "Name:{_name}\n",
    "Age:{_age}\n",
    "Job:{_job}'''.format(_name=name,_age=age,_job=job)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Mike\n",
      "age:28\n",
      "job:worker\n",
      "---------info of ---------\n",
      "Name:Mike\n",
      "Age:28\n",
      "Job:worker\n"
     ]
    }
   ],
   "source": [
    "# 第四种方法\n",
    "\n",
    "name=input(\"Name:\")\n",
    "age=int(input(\"age:\"))  #如果不用int()就会报错(虽然输入为数字，但是print(type(age))为str型)，因为python如果不强制类型转化，就会默认字符型\n",
    "job=input(\"job:\")\n",
    "\n",
    "info='''---------info of ---------\n",
    "Name:{0}\n",
    "Age:{1}\n",
    "Job:{2}'''.format(name,age,job)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.输入密码不可见"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入密码:········\n",
      "3434\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "pwd=getpass.getpass(\"请输入密码:\")\n",
    "print(pwd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.验证，python缩进"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:df\n",
      "password:i'\n",
      "Invalid username or password!\n"
     ]
    }
   ],
   "source": [
    "_username='Alex Li'\n",
    "_password='abc123'\n",
    "username=input(\"username:\")\n",
    "password=input(\"password:\")\n",
    "if _username==username and _password==password:\n",
    "    print((\"Welcome user {name} login...\").format(name=username))\n",
    "else:\n",
    "    print(\"Invalid username or password!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.指向---修改字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n",
      "Alex Li\n",
      "My name is Alex Li Alex Li\n",
      "My name is PaoChe Ge Alex Li\n",
      "您好，我来了\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello World\")\n",
    "name = \"Alex Li\"\n",
    "name2=name\n",
    "print(name)\n",
    "print(\"My name is\", name,name2) # Alex Li Alex Li\n",
    "name = \"PaoChe Ge\"\n",
    "# name2=name指的是name2与name一样指向Alex Li的内存地址，name指向改了，但是name2不变\n",
    "print(\"My name is\", name,name2) # PaoChe Ge Alex Li\n",
    "print(\"您好，我来了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.注释`''' '''`内涵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'print(\"这是一行注释\")'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一种情况就是注释\n",
    "'''print(\"这是一行注释\")'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是第一行内容\n",
      "这是第二行内容\n"
     ]
    }
   ],
   "source": [
    "#第二种情况就是打印多行字符串\n",
    "str='''这是第一行内容\n",
    "这是第二行内容'''\n",
    "print(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i'am a student\n"
     ]
    }
   ],
   "source": [
    "# 3.单套双，双套单都可以\n",
    "str1=\"i'am a student\"\n",
    "print(str1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.模块初始sys与os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['', 'C:\\\\ProgramData\\\\Anaconda3\\\\python36.zip', 'C:\\\\ProgramData\\\\Anaconda3\\\\DLLs', 'C:\\\\ProgramData\\\\Anaconda3\\\\lib', 'C:\\\\ProgramData\\\\Anaconda3', 'C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\site-packages', 'C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32', 'C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\site-packages\\\\win32\\\\lib', 'C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\site-packages\\\\Pythonwin', 'C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\site-packages\\\\IPython\\\\extensions', 'C:\\\\Users\\\\haigu\\\\.ipython']\n",
      "['C:\\\\ProgramData\\\\Anaconda3\\\\lib\\\\site-packages\\\\ipykernel_launcher.py', '-f', 'C:\\\\Users\\\\haigu\\\\AppData\\\\Roaming\\\\jupyter\\\\runtime\\\\kernel-28ddb76e-a36b-4edc-ab46-0b848ae5812f.json']\n",
      "C:\\Users\\haigu\\AppData\\Roaming\\jupyter\\runtime\\kernel-28ddb76e-a36b-4edc-ab46-0b848ae5812f.json\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "# 打印环境变量\n",
    "print(sys.path)\n",
    "\n",
    "print(sys.argv)\n",
    "print(sys.argv[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "##################################################"
     ]
    }
   ],
   "source": [
    "# 进度条\n",
    "import time\n",
    "for i in range(50):\n",
    "    sys.stdout.write('#')\n",
    "    sys.stdout.flush()\n",
    "    time.sleep(0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---> 0\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "cmd_res = os.system(\"dir\") # os.system()执行后直接输出到终端，然后结束，最后cmd_res保存的是os.system()执行后的状态码\n",
    "print(\"--->\",cmd_res) # ---> 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---> <os._wrap_close object at 0x000001B85DEF9940>\n"
     ]
    }
   ],
   "source": [
    "cmd_res1=os.popen(\"dir\")\n",
    "print(\"--->\",cmd_res1) # 得到的是内存对象值 ---> <os._wrap_close object at 0x00000000029187B8>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--->  驱动器 E 中的卷是 tools\n",
      " 卷的序列号是 E2B0-A3DA\n",
      "\n",
      " E:\\tools\\学术\\python\\两天入门python 的目录\n",
      "\n",
      "2018/12/15  11:10    <DIR>          .\n",
      "2018/12/15  11:10    <DIR>          ..\n",
      "2018/12/15  10:58    <DIR>          .ipynb_checkpoints\n",
      "2018/12/15  11:10             9,565 两天入门python.ipynb\n",
      "               1 个文件          9,565 字节\n",
      "               3 个目录 24,186,036,224 可用字节\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cmd_res1=os.popen(\"dir\").read()\n",
    "print(\"--->\",cmd_res1) # 读取数据必须再后面加个read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.mkdir(\"new_dir3\") # 创建一个目录\n",
    "os.removedirs(\"new_dir3\") # 删除一个目录"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.三元运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# 1.result = 值1 if 条件 else 值2\n",
    "a=1\n",
    "b=2\n",
    "c=3\n",
    "d=a if a>b else c\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.python3特性"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "python3中最重要的新特性大概是对文本和二进制数据作了更为清晰的区分。文本总是Unicode，由str类型表示,\n",
    "二进制数据则由bytes类型表示。Python3不会以任意隐式的方式混用str和bytes，正是这使得两者区分特别清晰。\n",
    "即：在python2中类型会自动转化，而在python3中则要么报错，要么不转化\n",
    "str与bytes相互转化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.bytes与str转化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我爱北京天安门\n"
     ]
    }
   ],
   "source": [
    "msg=\"我爱北京天安门\"\n",
    "\n",
    "print(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xe6\\x88\\x91\\xe7\\x88\\xb1\\xe5\\x8c\\x97\\xe4\\xba\\xac\\xe5\\xa4\\xa9\\xe5\\xae\\x89\\xe9\\x97\\xa8'\n"
     ]
    }
   ],
   "source": [
    "print(msg.encode(encoding=\"utf-8\")) # str转bytes,编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我爱北京天安门\n"
     ]
    }
   ],
   "source": [
    "print(msg.encode(encoding=\"utf-8\").decode(encoding=\"utf-8\")) # bytes转str,解码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11.循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一种循环\n",
      "count: 0\n",
      "count: 1\n",
      "count: 2\n",
      "count: 3\n",
      "count: 4\n",
      "count: 5\n",
      "count: 6\n",
      "count: 7\n",
      "count: 8\n",
      "count: 9\n"
     ]
    }
   ],
   "source": [
    "print(\"第一种循环\")\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"count:\",count)\n",
    "    count+=1\n",
    "    if(count==10):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第二种循环\n",
      "count: 0\n",
      "count: 2\n",
      "count: 4\n",
      "count: 6\n",
      "count: 8\n",
      "loop  0\n",
      "hehe....\n",
      "loop  1\n",
      "hehe....\n",
      "loop  2\n",
      "hehe....\n",
      "loop  3\n",
      "hehe....\n",
      "loop  4\n",
      "hehe....\n",
      "input your guess num:2\n",
      "Oops,think bigger!\n",
      "猜这么多次都不对，你个笨蛋.\n",
      "input your guess num:28\n",
      "Congratulations,you got it!\n"
     ]
    }
   ],
   "source": [
    "print(\"第二种循环\")\n",
    "count = 0\n",
    "for count in range(0,10,2):\n",
    "    print(\"count:\", count)\n",
    "\n",
    "for i in range(0,10):\n",
    "    if i<5:\n",
    "        print(\"loop \",i)\n",
    "    else:\n",
    "        continue\n",
    "    print(\"hehe....\")\n",
    "my_age=28\n",
    "count = 0\n",
    "while count<3:\n",
    "    user_input=int(input(\"input your guess num:\"))\n",
    "\n",
    "    if user_input==my_age:\n",
    "        print(\"Congratulations,you got it!\")\n",
    "        break\n",
    "    elif user_input<my_age:\n",
    "        print(\"Oops,think bigger!\")\n",
    "    else:\n",
    "        print(\"think smaller!\")\n",
    "    count+=1\n",
    "    print(\"猜这么多次都不对，你个笨蛋.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12.练习---三级菜单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "北京\n",
      "山东\n",
      "广东\n",
      "选择进入1>>:2\n",
      "北京\n",
      "山东\n",
      "广东\n",
      "选择进入1>>:3\n",
      "北京\n",
      "山东\n",
      "广东\n",
      "选择进入1>>:q\n",
      "北京\n",
      "山东\n",
      "广东\n",
      "选择进入1>>:2\n",
      "北京\n",
      "山东\n",
      "广东\n"
     ]
    }
   ],
   "source": [
    "data={\n",
    "    '北京':{\n",
    "        \"昌平\":{\n",
    "            \"沙河\":[\"oldboys\",'test'],\n",
    "            \"天通苑\":[\"链家地产\",\"我爱我家\"]\n",
    "        },\n",
    "        \"朝阳\":{\n",
    "            \"望京\":[\"oldboys\",'默陌陌'],\n",
    "            \"国贸\":[\"CICC\",\"HP\"],\n",
    "            \"东直门\":[\"Advent\",\"飞信\"]\n",
    "        },\n",
    "        \"海淀\":{}\n",
    "    },\n",
    "    '山东':{\n",
    "        \"德州\":{},\n",
    "        \"青岛\":{},\n",
    "        \"济南\":{}\n",
    "    },\n",
    "    '广东':{\n",
    "        \"德州\":{},\n",
    "        \"青岛\":{},\n",
    "        \"济南\":{}\n",
    "    },\n",
    "}\n",
    "exit_flag = False\n",
    "while not exit_flag:\n",
    "    for i in data:\n",
    "        print(i)\n",
    "    choice=input(\"选择进入1>>:\")\n",
    "    if choice in data:\n",
    "        while not exit_flag:\n",
    "            for i2 in data[choice]:\n",
    "                print(\"\\t\",i2)\n",
    "            choice2=input(\"选择进入2>>:\")\n",
    "            if choice2 in data[choice]:\n",
    "                while not exit_flag:\n",
    "                    for i3 in data[choice][choice2]:\n",
    "                        print(\"\\t\\t\", i3)\n",
    "                    choice3 = input(\"选择进入3>>:\")\n",
    "                    if choice3 in data[choice][choice2]:\n",
    "                        for i4 in data[choice][choice2][choice3]:\n",
    "                            print(i4)\n",
    "                        choice4=input(\"最后一层，按b返回>>:\")\n",
    "                        if choice4=='b':\n",
    "                            pass # pass可以理解为占位符，表示什么都不做，返回循环起始位置，以后可以在此处添加内容\n",
    "                        elif choice4=='q':\n",
    "                            exit_flag=True\n",
    "                    if (choice3 == 'b'):\n",
    "                        break\n",
    "                    elif choice3 == 'q':\n",
    "                        exit_flag = True\n",
    "            if (choice2 == 'b'):\n",
    "                break\n",
    "            elif choice2 == 'q':\n",
    "                exit_flag = True\n",
    "\n",
    "    if (choice == 'b'):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
